<?php

require_once("cfg/def.php");
require_once("cfg/cfg.php");
require_once("lib/logger.class.php");
require_once("lib/localestrings.class.php");
require_once("models/transfer.class.php");

include("inc/lang.php");

header('Content-Type: text/plain; charset=utf-8');

Logger::$log_file = USR_LOG_FILE;
Logger::Write("p_ajax", print_r($_GET, true));

define("ACT_ID", "DUMMY");

$loc = new LocaleStrings($lang, 'msg');

$act = strtoupper(get_item($_GET, "act", ACT_ID));
$phone = get_item($_GET, "phone", null);
switch($act)
{
    case "FIND":
        if(is_null($phone))
        {
            $result = array("STATE" => "ERROR", "MSG" => (string)$loc->get("msg", "no-phone"));
        }
        else
        {
            $msg = "";
            if(2==1 || wait_call($phone, $msg))
            {
                $t_obj = new Transfer();
                $t = $t_obj->find($phone);
                if(is_null($t))
                {
                    $result = array("STATE" => "ALERT", "MSG" => (string)$loc->get("msg", "no-phone", array("phone"=>$phone)));
                }
                else
                {
                    $result = array("STATE" => "OK", "MSG" => (string)$loc->get("msg", "sum-ready", array("sum"=>$t["money_sum"], "phone"=>$phone)), "id" => $t["id"]);
                }
            }
            else
                $result = array("STATE" => "TIMEOUT", "MSG" => $msg);
        }
        break;
    
    case "ADD":
        $phone1 = get_item($_GET, "phone1", null);
        $phone2 = get_item($_GET, "phone2", null);
        $sum = get_item($_GET, "sum", null);
        
        if(is_numeric($phone1) && is_numeric($phone2) && is_numeric($sum))
        {
            $t_obj = new Transfer();
            $t_obj->make((string)$phone1, (string)$phone2, (int)$sum);
            $result = array("STATE" => "OK", "MSG" => (string) $loc->get("msg", "money-sent"));

            call2("SMS", $phone2, (string)$loc->get('msg', 'money4you', array('sum'=>$sum, 'phone'=>$phone1)));
            call2("SMS", $phone1, (string)$loc->get('msg', 'money4him', array('sum'=>$sum, 'phone'=>$phone2)));
        }
        else
        {
            $result = array("STATE" => "ERROR", "MSG" => (string)$loc->get("msg", "send-error"));
        }
        
        break;
    
    case "GIVE":
        $transfer_id = get_item($_GET, "tid", null);
        if(is_null($transfer_id))
        {
            $result = array("STATE" => "ERROR", "MSG" => (string)$loc->get("msg", "send-error"));
        }
        else
        {
            $t_obj = new Transfer();
            $t_obj->give($transfer_id);
            $result = array("STATE" => "OK", "MSG" => "", "DESC" => "");
            $t = $t_obj->get2($transfer_id);
            
            call2("SMS", $t["phone1"], (string)$loc->get('msg', 'money-got', array('sum'=>$t["money_sum"], 'phone'=>$t["phone2"])));
        }
        break;
    
    default:
        $result = array("STATE" => "ALERT", "MSG" => (string)$loc->get('msg', 'data-error'));
        break;
}

echo json_encode($result);
exit();




function call2($act, $phone, $msg = null)
{
    global $lang;
    $url = PATH2CALL.'?act='.$act.'&lang='.$lang.'&timeout='.CALLWAIT_TIMEOUT_SEC.'&phone='.$phone;
    if(!is_null($msg)) $url .= '&msg='.urlencode($msg);
    Logger::Write("p_ajax->call2", $url);
    
    
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL, $url); // set url to post to  
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);  
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);// allow redirects  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable  
    curl_setopt($ch, CURLOPT_TIMEOUT, CALLWAIT_TIMEOUT_SEC + 120); // times out after 4s  
    curl_setopt($ch, CURLOPT_POST, 0); // set POST method  
    $result = curl_exec($ch); // run the whole process  
    curl_close($ch);   

    $json = json_decode($result, true);
    return $json;
}
	

function wait_call($phone, &$msg)
{
    $json = call2('identify', $phone);
    $msg = $json['MSG'];
    return $json['CODE'] == '200';
}


function get_item($items, $key, $def = null)
{
    if(is_null($items)) return $def;
    if(!is_array($items)) return $def;
    if(isset($items[$key]))
        return $items[$key];
    else
        return $def;
}
